package com.card.hwatt.controllers;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.card.hwatt.assembler.EmployeeAssembler;
import com.card.hwatt.service.EmployeeService;
import com.card.hwatt.dto.*;

/**
 * 员工
 * @author Administrator
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/employee")
public class EmployeeController extends BaseController{

	@Autowired
	@Qualifier("employeeService")
	private EmployeeService employeeService;

	@RequestMapping("/index")
	public String index() {
		return "employee";
	}

	@RequestMapping("/loadAll")
	public @ResponseBody
	List<Employee> loadAll(Long id, String key) {
		if (id == null) {
			return new ArrayList<Employee>();
		}

		List<Object> params = new ArrayList<Object>();

		params.add("%@" + id + "@%");

		if (StringUtils.isNotEmpty(key)) {
			params.add("%" + key + "%");
			params.add("%" + key + "%");
		}

		List<com.card.hwatt.entity.Employee> list = employeeService
				.loadAll(new PageParam(
						0,
						0,
						" department.path like ? "
								+ (StringUtils.isNotEmpty(key) ? " and ( name like ? or employeeCode like ? ) "
										: StringUtils.EMPTY), params,
						" employeeCode asc ")).rows;
		List<Employee> rows = EmployeeAssembler.convert(list);
		return rows;
	}

	// @RequestMapping("/loadAll")
	// public @ResponseBody
	// PageResult<Employee> loadAll(int page, int rows, String sort, String
	// order) {
	// PageParam param = new PageParam();
	// param.page = page;
	// param.rows = rows;
	// param.sort = sort + " " + order;
	// PageResult<com.card.hwatt.entity.Employee> list = employeeService
	// .loadAll(param);
	// PageResult<Employee> result = EmployeeAssembler.convert(list);
	// return result;
	// }

	// @RequestMapping("/save")
	// public @ResponseBody
	// ReturnValue save(@RequestJsonParam Employee chat) {
	// if (chat.id == null) {
	// chat.id = UUID.randomUUID();
	// }
	// chat.addedDate = new Date();
	// com.card.hwatt.entity.Employee entity = EmployeeAssembler.convert(chat);
	// chatService.save(entity);
	//
	// ReturnValue result = new ReturnValue();
	// result.message = "ok";
	// result.isSucess = true;
	// return result;
	// }

	@RequestMapping("/delete")
	public @ResponseBody
	ReturnValue delete(@RequestParam Long id) {
		employeeService.delete(id);
		ReturnValue result = new ReturnValue();
		result.message = "ok";
		result.isSucess = true;
		return result;
	}
}
